package com.jingxuan.config;

import com.jingxuan.service.ProductsTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 数据初始化组件
 * 在应用启动时自动执行数据初始化任务
 */
@Slf4j
@Component
public class DataInitializer implements ApplicationRunner {

    @Autowired
    private ProductsTypeService productsTypeService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("开始初始化应用数据...");
        
        // 初始化商品类型缓存
        try {
            productsTypeService.recordAllProductsType();
            Long typeCount = productsTypeService.getProductTypeCount();
            log.info("商品类型缓存初始化完成，共加载 {} 种类型", typeCount);
        } catch (Exception e) {
            log.error("商品类型缓存初始化失败", e);
            // 这里可以选择是否抛出异常阻止应用启动
            // throw e;
        }
        
        log.info("应用数据初始化完成");
    }
}

